package com.youka.bussiness.common.base;

import lombok.Getter;

/**
 * 异常基类
 *
 * @author cai
 */
@Getter
public class BaseException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    protected String code;
    /**
     * 错误消息
     */
    protected String message;
    /**
     * 是否展示
     */
    protected boolean show;
    /**
     * 错误对应的参数
     */
    protected Object[] args;

    public BaseException(String code, String message, boolean show, Object[] args) {
        this.code = code;
        this.message = message;
        this.show = show;
        this.args = args;
    }

    public BaseException(String code, String message, Object[] args) {
        this.code = code;
        this.message = message;
        this.show = true;
        this.args = args;
    }

    public BaseException(String code, String message, boolean show) {
        this.code = code;
        this.message = message;
        this.show = show;
    }

    public BaseException(String code, String message) {
        this.code = code;
        this.message = message;
        this.show = false;
    }

    public BaseException(String message) {
        this.code = "1";
        this.message = message;
        this.show = false;
    }

    public static String getCustomMessage(String format, Object... objects) {
        for (Object object : objects) {
            format = format.replaceFirst("\\{\\}", String.valueOf(object));
        }
        return format;
    }

}
